# encoding: utf-8
SUBJECTS = {
	:c => "语",
	:m => "数",
	:e => "英",
	:ph => "物"
}
HEADER = %w(学号 姓名 %-m/%-d)
FILENAME = "学习情况汇总"
days = [] # days[n].class # => Time
ar = {} # 索引为学号
names = File.read("name.list", :encoding => "utf-8").split(/\n/)
names.each_index do |i|
	names[i] = names[i].split(/`/)
	names[i][0] = names[i][0].to_i
	ar[names[i][0]] = {:name => names[i][1]}
end
Dir["*.txt"].each do |fn|
	txt = File.read(fn).split(/\n/)
	days << Time.new(*File.basename(fn, ".txt").split(/-/).map(&:to_i))
	txt.each do |l|
		w = l.split(/\s+/)
		subject = w.shift.downcase.to_sym
		w.each do |num|
			break if %w(nil undefined).include?(num)
			ar[num.to_i][subject] ||= []
			ar[num.to_i][subject] << days[-1]
		end
	end
end
h = HEADER.clone
time_format = h.pop
result = (h + days.map { |t| t.strftime(time_format) }).join(",")
result << "\n"
ar.each_pair do |num, hash|
	result << "#{num},#{hash[:name]}"
	e = {}
	%w(c m e ph).each do |ps|
		p = hash[ps.to_sym]
		Array(p).each do |date|
			e[date] ||= ""
			e[date] << SUBJECTS[ps.to_sym]
		end
	end
	days.each { |d| result << ",#{e[d]}" }
	result << "\n"
end
File.write("#{FILENAME}.csv", result, :encoding => "gbk")
